from DataAccess.DBConnFactory import *

from string import Template

def query_trading_dates_for_one_exchange(exch):

	sql_tpl = Template('''select TDATE from TRADEDATE where EXCHANGE='${EXCH}' ''')												
	sql_text = sql_tpl.substitute(EXCH = exch)
	
	cursor = DBConnFactory().get_db_connection('FINCHINA').cursor()
	cursor.execute(sql_text)
	r = cursor.fetchall()
	
	if r:
		return [d[0].date() for d in r]
	else:
		print '      err info...failed in querying trading dates,', exch
		
def query_trading_dates(exch_list):
	
	if exch_list:
	
		r = set(query_trading_dates_for_one_exchange(exch_list[0]))
		
		for exch in exch_list:
			r.intersection_update(set(query_trading_dates_for_one_exchange(exch)))
		
		r = list(r)
		r.sort()
		
		return r